package com.server.Service;

import com.server.entity.Types;
import com.server.mapper.TypesMapper;
import org.apache.ibatis.session.SqlSession;
import untils.MybatisClass;
import untils.Utils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Random;

/**
 * Discount type information service
 *
 * @author user
 */
public class TypesService {
    /**
     * Get all types list.
     * Return a list of all types of discount information
     *
     * @return the list Calling method: TypesService.getAllTypes()
     */
    public static List<Types> getAllTypes(){
        SqlSession sqlSession = MybatisClass.getSqlSession();
        TypesMapper typesMapper = sqlSession.getMapper(TypesMapper.class);
        List<Types> newList= typesMapper.selectByExample(null);
        sqlSession.close();
        return newList;
    }

    /**
     * Select by primary key Types show information.
     * Get information about a single piece of data
     *
     * @param id the id
     * @return the Types Calling method: TypesService.selectByPrimaryKey(id)
     */
    public static Types selectByPrimaryKey(Integer id){
        SqlSession sqlSession = MybatisClass.getSqlSession();
        TypesMapper typesMapper = sqlSession.getMapper(TypesMapper.class);
        Types types=typesMapper.selectByPrimaryKey(id);
        sqlSession.close();
        return types == null ?new Types():types;
    }

    /**
     * Instr vales types type int.
     *
     * @param number    the number
     * @param startTime the start time
     * @param entire    the entire
     * @return the int
     */
    public static int instrValesTypesType(int number, String startTime, String entire){
        int sumCount = 0;
        SqlSession sqlSession=MybatisClass.getSqlSession();
        TypesMapper typesMapper = sqlSession.getMapper(TypesMapper.class);
        for (int i = 0; i <number ; i++) {
            sumCount+=typesMapper.insert(new Types(Utils.getRandomString(12)+i,
                    BigDecimal.valueOf(new Random().nextInt(100) * 0.01),
                    Utils.randomDate(startTime, entire)
                    ));
            sqlSession.commit();
        }
        sqlSession.close();
        return sumCount;
    }

}
